package com.zshy.base.i18n.config;

import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;
import org.springframework.web.servlet.LocaleResolver;

import java.util.Locale;

/**
 * 获取请求头国际化信息
 *
 * @author zhouhengzhe
 */
public class I18nLocaleResolver implements LocaleResolver {

    /**
     * 解决语言转换问题
     *
     * @param httpServletRequest 请求
     * @return Locale
     */
    @Override

    public Locale resolveLocale( HttpServletRequest httpServletRequest) {
        String language = httpServletRequest.getHeader("content-language");
        //默认是中文
        Locale locale = Locale.CHINA;
        if (language != null && language.length() > 0) {
            String[] split = language.split("-");
            locale = new Locale(split[0], split[1]);
        }
        return locale;
    }

    /**
     * 设置语言
     *
     * @param httpServletRequest
     * @param httpServletResponse
     * @param locale
     */
    @Override
    public void setLocale(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse, Locale locale) {

    }
}
